package cn.jhc.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import cn.jhc.bean.Account;
import cn.jhc.bean.Title;
import cn.jhc.factory.ConnectionFactory;

public class AccountDAO {

	public List<Account> getAll(int userid) {
		List<Account> list = new ArrayList<>();
		Connection connection = ConnectionFactory.getConnection();
		final String sql = "select id,balance,creditcard from Account where userId = " +userid;
		Statement statement = null;
		ResultSet rs = null;

		try {
			statement = connection.createStatement();
			rs = statement.executeQuery(sql);
			while (rs.next()) {
				Account account = new Account();
				account.setId(rs.getInt(1));
				account.setBalance(rs.getDouble(2));
				account.setCreditcard(rs.getString(3));
				list.add(account);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				rs.close();
				statement.close();
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return list;
	}

	public void deleteAccount(int id) {
		Connection connection = ConnectionFactory.getConnection();
		Statement statement = null;

		final String sql = "delete from Account where id= " + id;

		try {
			statement = connection.createStatement();
			statement.execute(sql);

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				statement.close();
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
	}

	public boolean addAccount(Account account) {
		Statement stat = null;
		boolean value = true;
		Connection con = ConnectionFactory.getConnection();
		String sql = "insert into Account(balance,creditcard,userId) values("
				+ account.getBalance() + ",'" + account.getCreditcard() + "'," + account.getUserId() + ")";
		try {
			stat = con.createStatement();
			value = stat.execute(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			stat.close();
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return value;
	}

	public Account findById(int userId) {
		Account account = new Account();
		Connection connection = ConnectionFactory.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		final String sql = "select id,balance,creditcard from Account where userId = ?";
		try {
			ps = connection.prepareStatement(sql);
			ps.setInt(1, userId);
			rs = ps.executeQuery();
			if (rs.next()) {
				account.setId(rs.getInt(1));
				account.setBalance(rs.getDouble(2));
				account.setCreditcard(rs.getString(3));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				rs.close();
				ps.close();
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return account;
	}
	
	
	public  void updateAccount(Double balance, String creditcard ) {
		// TODO Auto-generated method stub
		Connection connection = ConnectionFactory.getConnection();
		Statement statement = null;
		String sql = "UPDATE Account set balance = balance - " + balance
				+ "where creditcard = " + creditcard;
		try {
			statement=connection.createStatement();
			statement.executeUpdate(sql);
		} catch (SQLException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		finally {
			try {
				statement.close();
				connection.close();
			} catch (Exception e) {
				// TODO: handle exception
			e.printStackTrace();
			}
		}
	}
}
